import MusicManager from "../../../MusicManager";
import DestructibleFlyer from "../../flyer/DestructibleFlyer";
import MoveComp from "../../funcComp/MoveComp";
import { Killer } from "../../funcObj/interface/HealthPoint";
import { Global } from "../../info/Global";
import Player from "../../player/Player";
import DefensiveFacility from "../DefensiveFacility";

const { ccclass, property } = cc._decorator;

/**
 * 黑洞发生器
 */
@ccclass
export default class BlackHoleGenerator extends DefensiveFacility {

    @property(cc.ProgressBar)
    creatProgress: cc.ProgressBar = null;

    @property(cc.Node)
    hole: cc.Node = null;

    creating: boolean;

    init(player: Player): void {
        super.init(player);
        this.hole.active = false;
        this.detectionCollider.enabled = false;
        this.creating = false;
        this.creatHole();
    }

    private creatHole() {
        if (this.creating) return;
        this.creatProgress.progress = 0;
        this.creatProgress.node.active = true;
        this.creating = true;
        cc.tween(this.creatProgress)
            .to(this.facilityInfo.冷却所需时间, { progress: 1 })
            .call(() => {
                this.hole.active = true;
                this.hole.getComponent(cc.Animation).play();
                this.creatProgress.node.active = false;
                this.creating = false;

                this.hole.scale = 0;
                this.hole.position = cc.v3(0, 30);
                cc.tween(this.hole)
                    .to(0.5, { scale: 0.4 })
                    .start();
            })
            .start();
    }

    onCollisionEnter(other: cc.Collider, self: cc.CircleCollider) {
        if (other.tag == 1 || self.tag == 0) return;
        let flyer = other.node.parent.getComponent(DestructibleFlyer);
        if (flyer && flyer.player !== this.player) {
            this.intrudersFlyer.push(flyer);
            let target = flyer.node.parent.convertToNodeSpaceAR(this.hole.convertToWorldSpaceAR(cc.Vec3.ZERO));

            flyer.getComponent(MoveComp).enabled = false;
            let angle = flyer.node.angle;
            flyer.node.angle += 360;
            cc.Tween.stopAllByTarget(flyer.node);
            cc.tween(flyer.node)
                .to(1, { position: target, angle: angle })
                .call(() => {
                    flyer.getComponent(MoveComp).enabled = true;
                    flyer.reducedHp({ facility: this, player: this.player, domage: flyer.maxHp * 2, flyer: null });
                })
                .start();
        }
    }

    onCollisionExit(other: cc.Collider, self: cc.Collider): void {
        if (other.tag == 1 || self.tag == 0) return;
        let flyer = other.node.parent.getComponent(DestructibleFlyer);
        if (flyer) {
            let index = this.intrudersFlyer.findIndex(item => item == flyer);
            if (index >= 0) {
                flyer.getComponent(MoveComp).enabled = true;
                cc.Tween.stopAllByTarget(flyer.node);
                this.intrudersFlyer.splice(index, 1);
            }
        }
    }

    public run() {
        if (!this.hole.active) return;
        MusicManager.instance.play(MusicManager.instance.blackHole, this.hole, true);
        this.detectionCollider.enabled = true;
        cc.tween(this.hole)
            .to(1, { scale: 3, position: cc.v3(0, 300) })
            .call(() => {
                this.scheduleOnce(this.close.bind(this), this.facilityInfo.持续时间);
            })
            .start();
    }

    private close() {
        this.detectionCollider.enabled = false;
        cc.tween(this.hole)
            .to(1, { scale: 0, position: cc.v3(0, 30) })
            .call(() => {
                this.hole.active = false;
                this.creatHole();
            })
            .start();
    }

    die(killer: Killer): void {
        this.hole.stopAllActions();
        this.unscheduleAllCallbacks();
        cc.Tween.stopAllByTarget(this.hole);
        cc.Tween.stopAllByTarget(this.creatProgress);
        super.die(killer);
    }
}
